<?php

class Options extends Controller {
	
	function Options()
	{
		parent::Controller();	
	}
	
	function index()
	{
		$this->load->helper('form');
		$this->load->database();
		
		$data['title'] = "GCMS";
		$data['heading'] = "Options";
		$data['style'] = base_url().'system/application/views/content.css';
		
		$data['header'] = $this->load->view('header', $data, true);
		$data['footer'] = $this->load->view('footer', $data, true);
		$this->_login_check($data, 'options_view');
	}	
	
	function add()
	{
		$this->load->database();
		$this->load->library('validation');
		
		$rules['option_name']	= "required";
		$rules['option_value']	= "required";
		$this->validation->set_rules($rules);
		
		$data = array(
               'option_name' => $_POST['option_name'],
               'option_value' => $_POST['option_value']
            );

		if($this->validation->run()){
			$this->db->insert('gcms_options', $data); 
		}
		
		redirect('/options/', 'refresh');
	}
	
	function edit()
	{
		$this->load->database();
		$query = $this->db->query('SELECT * FROM gcms_options');
		foreach ($query->result() as $row)
		{
			if($row->option_name != 'theme'){
				if($_POST['option'.$row->option_id] != ''){
					$data = array(
		               'option_value' => $_POST['option'.$row->option_id]
		            );
					$this->db->where('option_id', $row->option_id);
					$this->db->update('gcms_options', $data); 
				}
			}
		}
		
		redirect('/options/', 'refresh');
	}
	
	function delete($id)
	{
		$this->load->database();
		$this->db->where('option_id', $id);
		$this->db->delete('gcms_options'); 
		
		redirect('/options/', 'refresh');
	}
	
	function _login_check($data, $dest)
	{
		$this->load->library('session');
		
		$logged_in = $this->session->userdata('logged_in');
		if(!$logged_in){
			$this->load->view('login', $data);
		} else {
			$this->load->view($dest, $data);
		}
	}
}
?>